package com.open.capacity.user.controller;

import java.io.OutputStream;
import java.util.List;
import java.util.Map;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.open.capacity.user.model.SysPost;
import com.open.capacity.user.vo.SysRoleExcel;
import com.open.capacity.utils.StringUtil;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.apache.poi.ss.usermodel.Workbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.open.capacity.annotation.log.LogAnnotation;
import com.open.capacity.commons.PageResult;
import com.open.capacity.commons.Result;
import com.open.capacity.model.system.SysRole;
import com.open.capacity.user.service.SysRoleService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author 作者 ftquan E-mail: 464725602@qq.com
 * @version 创建时间：2017年11月12日 上午22:57:51
 * 角色管理
 */
@RestController
@Api(tags = "角色模块api")
public class SysRoleController {

    private static Logger logger = LoggerFactory.getLogger(SysRoleController.class);

    @Autowired
    private SysRoleService roleService;


    /**
     * 查询角色
     *
     * @param params
     * @return
     */
    @PreAuthorize("hasAuthority('role:get/roles')")
    @ApiOperation(value = "查询角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "分页起始位置", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "limit", value = "分页结束位置", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "keyword", value = "关键字", required = true, dataType = "String")
    })
    @GetMapping("/roles")
    public PageResult<SysRole> findRoles(@RequestParam Map<String, Object> params) {
        return roleService.pageRoles(params);
    }


    @ApiOperation(value = "获取角色名称")
    @PreAuthorize("hasAuthority('role:get/roles/getNames')")
    @GetMapping("/roles/getNames")
    public Result getRoleNames(@RequestParam(value = "keyValue") String keyValue) {
        String nameStr = "";
        String[] arrIds = keyValue.split(",");
        for (String id : arrIds) {
            SysRole role = roleService.findById(Long.parseLong(id));
            if (role != null) {
                if (StringUtil.isEmpty(nameStr)) {
                    nameStr += role.getName();
                } else {
                    nameStr += "," + role.getName();
                }
            }
        }
        return Result.succeed(nameStr, "获取成功！");
    }


    /**
     * 角色新增或者更新
     *
     * @param sysRole
     * @return
     */
    @PreAuthorize("hasAnyAuthority('role:post/roles','role:put/roles')")
    @PostMapping("/roles/saveOrUpdate")
    @LogAnnotation(module = "角色", name = "更新")
    public Result saveOrUpdate(@RequestBody SysRole sysRole, Long keyValue) {
        return roleService.saveOrUpdate(sysRole, keyValue);
    }

    /**
     * 角色授权审核
     *
     * @return
     */
    @PreAuthorize("hasAnyAuthority('role:post/roles','role:put/roles')")
    @PostMapping("/roles/auth")
    @LogAnnotation(module = "角色", name = "审核")
    public Result auth(Long keyValue) {
        int count = roleService.auth(keyValue);
        return Result.succeed("审核成功");
    }

    /**
     * 删除角色
     * delete /role/1
     *
     * @param id
     */
    @PreAuthorize("hasAuthority('role:delete/roles/{id}')")
    @ApiOperation(value = "删除角色")
    @DeleteMapping("/roles/{id}")
    @LogAnnotation(module = "角色", name = "删除")
    public Result deleteRole(@PathVariable(value = "id") Long id) {
        try {
            if (id == 1L) {
                return Result.failed("管理员不可以删除");
            }
            roleService.deleteRole(id);
            return Result.succeed("操作成功");
        } catch (Exception e) {

            return Result.failed("操作失败");
        }
    }
}
